package view;

import java.util.LinkedList;
import java.util.List;
import javax.swing.table.DefaultTableModel;

import resources.ResourceProvider;

import model.EmailImp;

/**
 *  Class EmailTableModel will display email(s) according to the folder selected 
 *
 */
public class EmailTableModel extends DefaultTableModel
{
    
    private static final long serialVersionUID = -2344485089990459042L;
     
    private String[] columnNames;
    private List<EmailImp> emailList;
    private String actionFolder;
    private List<Boolean> checkBoxList;

   /**
    *  Constructor
    * @param path the path
    * @param emails list of emails
    */
    public EmailTableModel(String path, List<EmailImp> emails)
    {
        this.actionFolder = ResourceProvider.getRootFolder(path);
        this.emailList = emails;
        this.columnNames = this.fetchColumnNames();
        this.checkBoxList = this.fetchCheckBox();
    }
    
    /**
     * @return CheckBox corresponding to each email
     */
    private List<Boolean> fetchCheckBox()
    {
        this.checkBoxList = new LinkedList<Boolean>();
        for(int n=0; n<this.emailList.size(); n++)
        {
            this.checkBoxList.add(false);
        }
        
        return this.checkBoxList;
    }

    /**
     * @return Column names according to folder selected i.e. how to display email display area according to folder selected.
     */
    private String[] fetchColumnNames()
    {
        if(ResourceProvider.INBOX.equals(this.actionFolder) || ResourceProvider.DRAFT.equals(this.actionFolder)
                || ResourceProvider.TRASH.equals(this.actionFolder))
        {
            String[] columnNames = {"","From", "To", "Subject", "Time"};
            return columnNames;
        }
        else if(ResourceProvider.OUTBOX.equals(this.actionFolder) || ResourceProvider.SENT.equals(this.actionFolder))
        {
            String[] columnNames = {"", "To", "Subject", "Time"};
            return columnNames;
        }
        
        String[] columnNames = {};
        return columnNames;
    }
    
   
    /*
     * (non-Javadoc)
     * @see javax.swing.table.DefaultTableModel#isCellEditable(int, int)
     */
    public boolean isCellEditable(int row, int col) 
    {
        if (col == 0) 
        {
            return true;
        } 
        else 
        {
            return false;
        }
    }
    
    /*
     * (non-Javadoc)
     * @see javax.swing.table.DefaultTableModel#setValueAt(java.lang.Object, int, int)
     */
    public void setValueAt(Object value, int row, int col)
    {         
        if(col == 0)
        {
           this.checkBoxList.set(row, (Boolean) value);
        }
        fireTableCellUpdated(row, col);
    }
    
    /*
     * (non-Javadoc)
     * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
     */
    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
        return getValueAt(0, columnIndex).getClass();
    }
    
    @Override
    public String getColumnName(int columnIndex)
    {
        return this.columnNames[columnIndex];
    }
/*
 * (non-Javadoc)
 * @see javax.swing.table.DefaultTableModel#getRowCount()
 */
    @Override
    public int getRowCount()
    {
        if(this.emailList == null)
        {
            return 0;
        }
        return this.emailList.size();
    }
/*
 * (non-Javadoc)
 * @see javax.swing.table.DefaultTableModel#getColumnCount()
 */
    @Override
    public int getColumnCount()
    {
        return columnNames.length;
    }
    
    /*
     * (non-Javadoc)
     * @see javax.swing.table.DefaultTableModel#getValueAt(int, int)
     */
    @Override
    public Object getValueAt(int rowIndex, int columnIndex)
    {
        EmailImp email = (EmailImp) emailList.get(rowIndex);
        
        if(ResourceProvider.INBOX.equals(this.actionFolder) || ResourceProvider.DRAFT.equals(this.actionFolder)
                || ResourceProvider.TRASH.equals(this.actionFolder))      
        {
            switch (columnIndex) 
            {
                case 0:
                    return this.checkBoxList.get(rowIndex);
                case 1:
                    return email.getSender();
                case 2:
                    if(email.getReceiver().size() > 0)
                    {
                        return email.getReceiver().get(0);
                    }
                    
                    return "";
                case 3:
                   return email.getSubject();
                case 4:
                   return email.getTime();
                default:
                   return new Object();
            }
        }
        else if(ResourceProvider.OUTBOX.equals(this.actionFolder) || ResourceProvider.SENT.equals(this.actionFolder))
        {
            switch (columnIndex) 
            {
                case 0:
                    return this.checkBoxList.get(rowIndex);
                case 1:
                    if(email.getReceiver().size() > 0)
                    {
                        return email.getReceiver().get(0);
                    }
                    
                    return "";
                case 2:
                   return email.getSubject();
                case 3:
                   return email.getTime();
                default:
                   return new Object();
            }
        }
        
        return "";     
    }
    
    /**
     * get selected emails
     * @return list of email(s)
     */
    public List<EmailImp> getSelectedMails()
    {
        List<EmailImp> selectedMails = new LinkedList<EmailImp>();
        for(int n=0; n<this.checkBoxList.size();n++)
        {
            Boolean isSelected = this.checkBoxList.get(n);
            if(isSelected)
            {
                selectedMails.add(this.emailList.get(n));
            }
        }
        return selectedMails;
    }
    
    /**
     * get email associated with the give row
     * @param row the given row
     * @return the email in the given row
     */
    public EmailImp getEmailAssociatedToRow(int row)
    {
        return this.emailList.get(row);
    }
}
